package com.credithc.customers.dataservice.service.impl;

import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.dataservice.Thread.OldDataThreadPoolExecutor;
import com.credithc.customers.dataservice.constants.SyncTypeEnum;
import com.credithc.customers.dataservice.mapper.XD.MicroloanUserMapper;
import com.credithc.customers.dataservice.po.GdAccountPo;
import com.credithc.customers.dataservice.ro.CustomerAccountInfoRo;
import com.credithc.customers.dataservice.service.OldXdDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;

@Service
@Slf4j
public class OldXdDataServiceImpl implements OldXdDataService {
    @Autowired
    private MicroloanUserMapper microloanUserMapper;
    @Autowired
    private SyncDataServiceImpl syncDataService;
    @Autowired
    private OldDataThreadPoolExecutor oldDataThreadPoolExecutor;
    @Override
    public void oldData() {
        boolean f =true;
        Long num = microloanUserMapper.queryProductMaxId();
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+1001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-1000);
                    }
                    String param = Long.toString(vernier.get());

                    List<GdAccountPo> gdCustomerProductPOS = microloanUserMapper.queryDlrProduct(param);
                    if (gdCustomerProductPOS.size() > 0) {
                        bulidXdAccount(gdCustomerProductPOS);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    private void bulidXdAccount(List<GdAccountPo> gdCustomerProductPOS) {
        List<CustomerAccountInfoRo> customerAccountInfoRos = new ArrayList<>();
        for (GdAccountPo xd:gdCustomerProductPOS) {
            try {
                CustomerAccountInfoRo customerAccountInfoRo = new CustomerAccountInfoRo();
                customerAccountInfoRo.setAccountType(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_BANK_CARD);
                /*if ("6".equals(xd.getAccountType())){
                    customerAccountInfoRo.setSubSystemSign("CCXD");
                }else if ("8".equals(xd.getAccountType())){
                    customerAccountInfoRo.setSubSystemSign("BJXD");
                }else{
                    customerAccountInfoRo.setSubSystemSign("XD");
                }*/
                customerAccountInfoRo.setSubSystemSign(xd.getSubSystemSign());
                customerAccountInfoRo.setAccountNo(xd.getAccountNo());
                customerAccountInfoRo.setUserSourceId(xd.getUserSourceId());
                customerAccountInfoRo.setSystemSign("XD");
                customerAccountInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
                customerAccountInfoRo.setAccountUpdateTime(StringUtils.isNotBlank(xd.getAccountUpdateTime())?new Timestamp(Long.valueOf(xd.getAccountUpdateTime())):null );
                customerAccountInfoRo.setAccountCreateTime(StringUtils.isNotBlank(xd.getAccountCreateTime())?new Timestamp(Long.valueOf(xd.getAccountCreateTime())):null);
                customerAccountInfoRo.setCustomerNo(xd.getCustomerNo());
                customerAccountInfoRo.setCustomerRole(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LOAN);
               // customerAccountInfoRo.setAccountStatus(StringUtils.isNotBlank(xd.getAccountNo())? CustomerConstants.STATUS_ACCOUNT_AVAILABLE:CustomerConstants.STATUS_ACCOUNT_UNAVAILABLE);
                customerAccountInfoRo.setAccountStatus(xd.getBind_state() == 300 ?CustomerConstants.STATUS_ACCOUNT_AVAILABLE:CustomerConstants.STATUS_ACCOUNT_UNAVAILABLE);
                if (StringUtils.isNotBlank(customerAccountInfoRo.getCustomerNo()) && !"0".equals(customerAccountInfoRo.getCustomerNo())) {
                    customerAccountInfoRos.add(customerAccountInfoRo);
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        syncDataService.syncCustomerAccount(customerAccountInfoRos);
    }
}
